package com.inossem.warehousestorage_inossem.activity.in_storage.chuangyou;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.inossem.loadandrefresh.PullToRefreshLayout;
import com.inossem.loadandrefresh.pullableview.PullableListView;
import com.inossem.utils.listener.NoDoubleClickListener;
import com.inossem.utils.log.LogUtils;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.activity.base.BaseActivity;
import com.inossem.warehousestorage_inossem.activity.in_storage.extra.InStorageExtra;
import com.inossem.warehousestorage_inossem.activity.inventory.InventoryExtra;
import com.inossem.warehousestorage_inossem.activity.inventory.InventoryItemAddActivity;
import com.inossem.warehousestorage_inossem.activity.out_storage.extra.OutStorageExtra;
import com.inossem.warehousestorage_inossem.adapter.inStorage.chuangyou.CYCInStorageSerialNumberAdapter;
import com.inossem.warehousestorage_inossem.bean.outStorage.SNBean;
import com.inossem.warehousestorage_inossem.bean.outStorage.SNItemInfo;
import com.inossem.warehousestorage_inossem.utils.ActivityUtils;
import com.inossem.warehousestorage_inossem.utils.CYInStorageUtils;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * TODO
 *
 * @author 王斯宇
 * @time on 2018/3/23 14:10
 * @email siyu.wang@inossem.com
 */
public class CYInStorageSerialNumberActivity extends BaseActivity {
    private TextView title;

    private PullToRefreshLayout refresh;

    private PullableListView materielList;

    private CYCInStorageSerialNumberAdapter adapter;

    private String matId , packagingCode;

    private List<SNItemInfo> mSNList;

    private List<SNItemInfo> deleteList;


    @Override
    protected void onCreateImpl(Bundle savedInstanceState) {
        super.onCreateImpl(savedInstanceState);
        setContentView(R.layout.cy_activity_in_storage_serial_number);
        initView(CYInStorageSerialNumberActivity.this);
        initData(CYInStorageSerialNumberActivity.this);
    }

    @Override
    protected void onResumeImpl() {
//        getDataFromLocal(CYChangeStorageMaterielListActivity.this);
    }

    private void initView(Activity activity) {
        title = (TextView) activity.findViewById(R.id.headerTitle);
        materielList = (PullableListView) activity.findViewById(R.id.materielList);
        refresh = (PullToRefreshLayout) activity.findViewById(R.id.materielListRefresh);
    }

    private void initData(Activity activity) {
        title.setText(R.string.cy_activity_change_storage_material_detail_serial_number);
        ActivityUtils.initBackAndFunction(activity, getResources().getString(R.string.activity_receive_result_back), new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                Intent intent = new Intent();
                intent.putExtra(InStorageExtra.DELETE_SN_LIST, (Serializable) deleteList);
                intent.putExtra(InStorageExtra.SN_LIST, (Serializable) mSNList);
                intent.putExtra(InStorageExtra.PACKAGING_CODE , packagingCode);
                setResult(InStorageExtra.REQUEST_CODE, intent);
                finish();
            }
        });
        getExtra(activity);
        deleteList = new ArrayList<>();
        materielList.setPullUpFlag(false);
        materielList.setPullDownFlag(false);
        addChangeStorageMaterielListData(activity, materielList, true);
    }
    private void getExtra(Activity activity) {
        mSNList = (List<SNItemInfo>) activity.getIntent().getSerializableExtra(InStorageExtra.SN_LIST);
        packagingCode = activity.getIntent().getStringExtra(InStorageExtra.PACKAGING_CODE);
    }
    private void addChangeStorageMaterielListData(Context context, ListView listView, boolean flag) {
        if (flag) {
            adapter = new CYCInStorageSerialNumberAdapter(context , mSNList , new CYInStorageSerialNumberListener() {
                @Override
                public void onDeleteClickListener(String id , String code) {
                    SNItemInfo bean = new SNItemInfo(id , code);
                    deleteList.add(bean);
                    for (int i = 0; i < mSNList.size(); i++) {
                        if (mSNList.get(i).getCode().equals(code)) {
                            mSNList.remove(i);
                            break;
                        }
                    }
                    addChangeStorageMaterielListData(context , listView , true);
                }
            });
            listView.setAdapter(adapter);
        }
    }

    public interface CYInStorageSerialNumberListener {
        void onDeleteClickListener (String id , String code);
    }

    @Override
    public void onBackPressed() {
//        super.onBackPressed();
    }
}
